package com.zhongfl.crm.event.register;

import com.zhongfl.crm.event.LxChannelUpdateEvent;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 励销渠道创建事件处理器注册类
 *
 * @author 木叶丸
 * @date 2019/03/05
 */
@Service
public class LxChannelUpdateEventHandlerRegister implements ApplicationContextAware {

    private List<LxChannelUpdateEvent> lxChannelUpdateEvents = new ArrayList<>();

    public void register(LxChannelUpdateEvent lxChannelUpdateEvent){
        lxChannelUpdateEvents.add(lxChannelUpdateEvent);
    }

    public List<LxChannelUpdateEvent> getLxChannelUpdateEvents(){
        return this.lxChannelUpdateEvents;
    }

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @PostConstruct
    public void postContract(){
        Map<String, LxChannelUpdateEvent> beansOfType = applicationContext.getBeansOfType(LxChannelUpdateEvent.class);
        for (Map.Entry<String, LxChannelUpdateEvent> stringLxChannelUpdateEventEntry : beansOfType.entrySet()) {
            LxChannelUpdateEvent handler = stringLxChannelUpdateEventEntry.getValue();
            lxChannelUpdateEvents.add(handler);
        }
    }

}
